﻿using System;
using Microsoft.Practices.ObjectBuilder;
using Common;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using System.Globalization;

namespace CSC440TeamOne.Admin.Views
{
	public partial class ToleranceConfigList : Microsoft.Practices.CompositeWeb.Web.UI.Page, IToleranceConfigListView
	{
		private ToleranceConfigListPresenter _presenter;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
			{
				this._presenter.OnViewInitialized();
			}
			this._presenter.OnViewLoaded();
		}

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            repHealthParameers.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(repHealthParameers_ItemDataBound);
        }

        void repHealthParameers_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            ControlBinder<RepeaterItem, HealthParameter>.Bind(e.Item, a =>
            {
                a.Find<HyperLink>("btnEditHealthParameter").Text = a.BusinessItem.Name;

                a.Find<HyperLink>("btnEditHealthParameter").NavigateUrl = string.Format(CultureInfo.InvariantCulture, "ToleranceConfigDetails.aspx?ID={0}", a.BusinessItem.ID);

                a.Find<HyperLink>("btnEditHealthParameter").ToolTip = a.BusinessItem.Description;
            });
        }

		[CreateNew]
		public ToleranceConfigListPresenter Presenter
		{
			get
			{
				return this._presenter;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException("value");

				this._presenter = value;
				this._presenter.View = this;
			}
		}

		// TODO: Forward events to the presenter and show state to the user.
		// For examples of this, see the View-Presenter (with Application Controller) QuickStart:
		//	


        public void Show(Common.ToleranceConfigListViewModel model)
        {
            IEnumerable<HealthParameter> parameters = model.HealthParameters;

            if (parameters.Any())
            {
                mvState.SetActiveView(vwData);

                repHealthParameers.DataSource = parameters;

                repHealthParameers.DataBind();
            }
        }
    }
}

